//
//  LiveStream+MultipartFormEncoding.swift
//  Link
//
//  Created by melvyn on 2018/8/20.
//  Copyright © 2018 NEET. All rights reserved.
//

import Foundation
import Alamofire


extension LiveStream {
    
    public struct MultipartForm {
        public var key: String
        public var tag: String
        public var title: String
        public var metadata: Data
        
        struct Field {
            static let key = "key"
            static let tag = "tag"
            static let title = "title"
            static let metadata = "metadata"
        }
        
        public init(
            key: String,
            tag: String,
            title: String,
            metadata: Data) {
            self.key = key
            self.tag = tag
            self.title = title
            self.metadata = metadata
        }
    }
}

extension MultipartFormData {
    
    func encode(_ metadata: LiveStream.MultipartForm) {
        
        append(metadata.key.data(using: .utf8)!, withName: LiveStream.MultipartForm.Field.key, mimeType: "plain/text")

        append(metadata.tag.data(using: .utf8)!, withName: LiveStream.MultipartForm.Field.tag, mimeType: "plain/text")

        append(metadata.title.data(using: .utf8)!, withName: LiveStream.MultipartForm.Field.title, mimeType: "plain/text")

        append(metadata.metadata, withName: LiveStream.MultipartForm.Field.metadata, mimeType: "image/jpeg")
    }
}
